<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="产地" prop="factoryId">
        <el-select v-model="queryParams.factoryId" placeholder="请选择" filterable clearable @change="factoryChange">
          <el-option v-for="team in factoryList" :key="team.deptId" :label="team.deptName" :value="team.deptId" />
        </el-select>
      </el-form-item>
      <el-form-item label="班组" prop="teamId">
        <el-select v-model="queryParams.teamId" placeholder="请选择" clearable>
          <el-option v-for="team in teamList" :key="team.deptId" :label="team.deptName" :value="team.deptId" />
        </el-select>
      </el-form-item>
      <!--<el-form-item label="站点ID" prop="stationId">
        <el-input
          v-model="queryParams.stationId"
          placeholder="请输入站点ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="上报日期" prop="submitDate">
<!--        <el-date-picker @change="datechange" clearable v-model="queryParams.submitDate" type="date"-->
<!--          value-format="yyyy-MM-dd" placeholder="请选择上报日期">-->
<!--        </el-date-picker>-->
        <el-date-picker
          v-model="submitDateRange"
          @change="datechange"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['mobile:stationday:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading"
              border style="width:100%;" :data="stationdayList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="上报日期" align="center" prop="submitDate" width="180">
      </el-table-column>
      <el-table-column label="订单合同号" align="center" prop="contractCode" width="120" />
      <el-table-column label="生产工单号" align="center" prop="workerorderCode" width="130"  />
      <el-table-column label="成品编码" align="center" prop="bomCode" />
      <el-table-column label="成品名称" align="center" prop="bomName" />
      <el-table-column label="班次" align="center" prop="shiftTypeName" />
      <el-table-column label="组别" align="center" prop="teamName" />
<!--      <el-table-column label="产品名称" align="center" prop="bomName" />-->
      <el-table-column label="站点名称" align="center" prop="stationName" />
      <el-table-column label="出勤人数" align="center" prop="workNum" />
      <el-table-column label="工作时间" align="center" prop="workHoure" />
      <el-table-column label="生产数量" align="center" prop="makeNum" />
      <el-table-column label="良品数量" align="center" prop="goodNum" />
      <el-table-column label="不合格数量" align="center" prop="ungoodNum" />
      <el-table-column label="剩余未完成数量" align="center" prop="remainingCompletionNum" />
      <el-table-column label="UPPH" align="center" prop="upph" />
      <el-table-column label="出勤说明" align="center" prop="attendanceDesc" />

    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />


  </div>
</template>

<script>
import { listExportStationday, getStationday, delStationday, addStationday, updateStationday } from "@/api/sm/mobile/stationday";
import { listTeam, listFactory } from "@/api/system/dept";
import { formatDate } from "@/utils/index";
import moment from 'moment';
export default {
  name: "Stationday",
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      submitDateRange: this.getDefaultDateRange(),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 站点生产数据上报表格数据
      stationdayList: [],
      factoryList: [],
      teamList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        factoryId: null,
        teamId: null,
        stationId: null,
        createDate: null,
        submitDate: "",
        beginTime: null,
        endTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderCode: [
          { required: true, message: "订单编码不能为空", trigger: "blur" }
        ],
        workorderId: [
          { required: true, message: "工单ID不能为空", trigger: "blur" }
        ],
        bomUnit: [
          { required: true, message: "单位不能为空", trigger: "blur" }
        ],
        makeNum: [
          { required: true, message: "生产数量不能为空", trigger: "blur" }
        ],
        goodNum: [
          { required: true, message: "良品数量不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getFactoryList();

  },
  methods: {
    getDefaultDateRange() {
      const today = new Date();
      const tomorrow = new Date(today);
      tomorrow.setDate(tomorrow.getDate());

      // 将日期格式化为 yyyy-MM-dd
      const formatDate = (date) => {
        const year = date.getFullYear();
        const month = String(date.getMonth() + 1).padStart(2, '0');
        const day = String(date.getDate()).padStart(2, '0');
        return `${year}-${month}-${day}`;
      };

      return [formatDate(today), formatDate(tomorrow)];
    },
    dateStrings() {
      if (this.submitDateRange && this.submitDateRange.length === 2) {
        return this.submitDateRange.map(date => moment(date).format('YYYY-MM-DD'));
      }
      return ['', ''];
    },
    datechange(event) {
      // console.log(event);
      // this.queryParams = this.addDateRange(this.queryParams, this.dateRange);
      // console.log(this.queryParams);
    },
    getFactoryList() {
      this.loading = true;
      listFactory({ "level": 2 }).then(response => {
        this.factoryList = response.data;
        this.queryParams.factoryId = this.factoryList[0].deptId;
        this.getTeamList(this.queryParams.factoryId);
        this.loading = false;
      });
    },
    factoryChange() {
      this.getTeamList(this.queryParams.factoryId);
    },
    /** 查询订单信息列表 */
    getTeamList(parentId) {
      this.loading = true;
      listTeam({ "parentId": parentId }).then(response => {
        this.teamList = response.data;
        this.loading = false;
      });
    },
    /** 查询站点生产数据上报列表 */
    getList() {
      this.loading = true;
      let beginEndTime = this.dateStrings();
      this.queryParams.beginTime = beginEndTime[0];
      this.queryParams.endTime = beginEndTime[1];
      listExportStationday(this.queryParams).then(response => {
        this.stationdayList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderCode: null,
        workerorderCode: null,
        workorderId: null,
        factoryName: null,
        factoryId: null,
        teamName: null,
        teamId: null,
        stationId: null,
        stationName: null,
        bomCode: null,
        bomName: null,
        bomUnit: null,
        makeNum: null,
        goodNum: null,
        createDate: null,
        creator: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加站点生产数据上报";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStationday(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改站点生产数据上报";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStationday(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStationday(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除站点生产数据上报编号为"' + ids + '"的数据项？').then(function () {
        return delStationday(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mobile/stationday/export', {
        ...this.queryParams
      }, `stationday_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
